import { Connector } from "cax-connector";
import {
  API_EVENT,
  FileLoadOptions,
  IReq,
  IRes,
  MODEL_TYPE,
  TEMPLATE_TYPE,
} from "../interface";
import { v4 as uuidv4 } from "uuid";
import { devtool } from "cax-base";

export function file_load(
  this: Connector,
  type: MODEL_TYPE = MODEL_TYPE.GY,
  params: FileLoadOptions
  // {
  //   path: string,
  //   type: MODEL_TYPE = MODEL_TYPE.GY,
  //   append = false,
  //   importType = TEMPLATE_TYPE.GEOMETRY
  // }
): Promise<any> {
  if (this.client == null) return Promise.reject("socket未初始化");
  const req: IReq = {
    Topic: "api",
    Type: API_EVENT.FILE_LOAD,
    Params: params,
    Req: 1,
    Res: 0,
    Tag: uuidv4(),
  };
  switch (type) {
    case MODEL_TYPE.GY:
      this.client.emit("api", req);
      return new Promise((resolve, reject) => {
        this.client?.once(req.Tag, (ev: IRes) => {
          devtool.warn("新版file_load返回", ev);
          resolve(ev.Result);
        });
      });
    default:
      return Promise.reject();
  }
}
